package com.example.pdm_project_group_001;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

public class HttpConnect {

	private static String domain	= "192.168.1.5/WebService.asmx";
	private static String service	= "updateBusRoutes";
	
	HttpClient httpclient = new DefaultHttpClient();
	HttpPost httppost;
	HttpResponse response;
	HttpEntity entity;
	
	private static final String NAMESPACE = "http://tempuri.org/";
	//private static final String URL =Settings_.serverIP + "WebService.asmx";
	static SoapEnvelope rpc = new SoapEnvelope(SoapEnvelope.VER11);
	
	private static final String SOAP_ACTION = "http://tempuri.org/"+service;
	private static final String METHOD_NAME = service;

	
	private static String connectHttp(String address){
		String response="teek";
		
		
//		HttpClient client = new DefaultHttpClient();
//        HttpGet get = new HttpGet(address);        
//        ResponseHandler<String> responseHandler = new BasicResponseHandler();
//        try {               
//            response = client.execute(get,responseHandler);
//        } 
//        catch (Exception e) {
//            Log.e("RESPONSE", "is "+e.getMessage());                
//        }
		try {
		InputStream inputStream = null;
        String result = "";
        String url = "http://192.168.1.2/WebService.asmx/updateBusRoutes?id=3";
        
// 
//            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();
            Log.d("aaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbb");
//            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// 
//            // receive response as inputStream
            
            //Log.d("aaaaa",  httpResponse.toString() );
            
            //inputStream = httpResponse.getEntity().getContent();
            //response = inputStream.toString();
// 
//            // convert inputstream to string
//            
// 
        } catch (Exception e) {
            Log.d("InputStream", "...."+e.getLocalizedMessage());
        }
// 
		
        return response;
	}
	
	public static String updateBusHalts(){
		        
        
		return "aaaaaaaaaaaaaaaaaaa";//sendData("1");
	}
	
	
	public static String sendData(String id) {
		SoapObject request = new SoapObject(NAMESPACE, service);
		request.addProperty("id", id);
		
		HttpTransportSE service = new HttpTransportSE(domain);

		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		
		envelope.setOutputSoapObject(request);
		envelope.dotNet=true;
		
		//rpc.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(domain);
		String token = null;
		try {

			androidHttpTransport.call("http://tempuri.org/"+service, envelope);
			SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;


			token = resultsRequestSOAP.toString();
			System.out.println("Token : " + token);
			return token.substring(token.indexOf("=")+1 , token.lastIndexOf(";"));
		} catch (Exception e) {
			System.out.println("CATCH BLOCK ERROR____________________________________");
			e.printStackTrace();
			return "-1";
		}
	}

}
